﻿@using CQIE.TES.Model
@using CQIE.TES.WEB.USER.Models

<select name="mySelect">
    <option value="">请选择评教</option>
    @{
        foreach (EvaluaHeight item in ViewBag.EvaList)
        {

            <option value="@item.ID">@item.Name</option>
        }
    }
</select>
<button type="button"
        class="layui-btn layui-btn-normal" onclick="MakeSure()">
    确定
</button>
<form id="myForm" style="display: none;" class="layui-form" lay-filter="myForm">
    @{
        foreach (Question_Option item in ViewBag.showData)
        {
            <div class="layui-form-item">
                <div class="question">
                    @item.Info
                </div>
                <div>
                    <ul class="que" id="que">
                        @foreach (Option o in item.Options)
                        {
                            <li class="frm">
                                <input type="radio" name="@(item.ID)" value="@o.Score" title="@o.Content">
                            </li>
                        }
                    </ul>
                </div>
            </div>
        }
    }
    <Button lay-submit lay-filter="myformSub" class="layui-btn layui-btn-normal btn">提交</Button>
</form>


<script>
    //确认函数
    var MakeSure;

    //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use(['element', 'layer', 'table', 'jquery', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;

        //确定按钮
        MakeSure = function () {
            
            let evaId = $("[name='mySelect']").val();
            //验证为空
            if (evaId == "") {
                layer.msg('请选择评教', { icon: 2 });
                return;
            }
            var index = layer.load();
            $('#myForm').css('display', 'block');
            //清空选项
            $('#myForm')[0].reset();

            //将选择的select选项保存到sessionStorage中
            sessionStorage.setItem("evaId", evaId);
            $.get("/api/eva/teacherselfisrecorded",
                {
                    evaId: sessionStorage.getItem("evaId"),
                    teacherId: sessionStorage.getItem('tId')
                },
                function(res) {
                    if (res != null) {
                        //完成评教任务
                        console.log(res);
                        //转换obj
                        let obj = JSON.parse(res);
                        console.log(obj);
                        let data = {}
                        for (let i = 0; i < obj.length; i++) {
                            data[obj[i].qid] = obj[i].aid;
                        }
                        console.log(data);
                        form.val("myForm", data);
                        $('.btn').css('display', 'none');
                        form.render();
                    } else {
                        //未完成评教
                        $('.btn').css('display', 'block');
                        // 清空表单
                        $('#myForm')[0].reset();
                    }
                    layer.close(index);
                });

        }

        //提交
        form.on("submit(myformSub)",
            function(obj) {

                var data2 = form.val("myForm");
                console.log(Object.keys(data2).length, @ViewBag.Count);
                //判断是否题目全部选择了
                if (Object.keys(data2).length !== @ViewBag.Count) {
                    layer.msg('请完成所有题目', { icon: 2 });
                    return false;
                }
                //提交加载
                var index = layer.load();

                $.post("/api/eva/teacherself/submit",
                    {
                        evaId: sessionStorage.getItem("evaId"),
                        teacherId: sessionStorage.getItem('tId'),
                        answerIds: Object.values(data2),
                        questionIds: Object.keys(data2)
                    },
                    function(obj) {
                        //关闭加载
                        layer.close(index);
                        if (obj) {
                            layer.msg("提交成功！", { icon: 1 });
                            $.get("/api/eva/teacherselfisrecorded",
                                {
                                    evaId: sessionStorage.getItem("evaId"),
                                    teacherId: sessionStorage.getItem('tId')
                                },
                                function(res) {
                                    if (res != null) {
                                        //完成评教任务
                                        console.log(res);
                                        //转换obj
                                        let obj = JSON.parse(res);
                                        console.log(obj);
                                        let data = {}
                                        for (let i = 0; i < obj.length; i++) {
                                            data[obj[i].qid] = obj[i].aid;
                                        }
                                        console.log(data);
                                        form.val("myForm", data);
                                        $('.btn').css('display', 'none');
                                        form.render();
                                    } else {
                                        //未完成评教
                                        $('.btn').css('display', 'block');
                                        // 清空表单
                                        $('#myForm')[0].reset();
                                    }
                                    layer.close(index);
                                });
                        } else {
                            layer.msg("失败了~好像出了一些问题！", { icon: 2 });
                        }
                    });
                return false;
            });
    });

</script>
